package com.jshcbd.mvp_kt_demo.http

import com.jshcbd.mvp_kt_demo.app.MyApplication
import com.jshcbd.mvp_kt_demo.utils.NetUtil.isNetworkConnected
import okhttp3.CacheControl
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/*** 网络缓存过滤器设置  */
class NetworkInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        var request: Request = chain.request()

        //无网络时强制使用缓存
        if (!isNetworkConnected(MyApplication.application)) {
            request = request.newBuilder()
                .cacheControl(CacheControl.FORCE_CACHE)
                .build()
        }
        val response: Response = chain.proceed(request)
        if (isNetworkConnected(MyApplication.application)) {
            // 有网络时，设置超时为0
            val maxStale = 0
            response.newBuilder()
                .header("Cache-Control", "public, max-age=$maxStale")
                .removeHeader("Pragma") // 清除头信息，因为服务器如果不支持，会返回一些干扰信息，不清除下面无法生效
                .build()
        } else {
            // 无网络时，设置超时为3周
            val maxStale = 60 * 60 * 24 * 21
            response.newBuilder()
                .header("Cache-Control", "public, only-if-cached, max-stale=$maxStale")
                .removeHeader("Pragma")
                .build()
        }
        return response
    }
}